package com.jiaz.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jiaz.dto.CategoryBaseDTO;
import com.jiaz.entity.Category;
import com.jiaz.entity.CheckIn;
import com.jiaz.mapper.CategoryMapper;
import com.jiaz.service.CategoryService;
import com.jiaz.untils.ResponseBean;
import com.jiaz.untils.constant.VarConstant;
import com.jiaz.vo.QueryVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @author jiajiazi
 * @version 1.0
 * @date 2022/4/24 16:18
 */
@Service
public class CategoryServiceImpl implements CategoryService {

    @Autowired
    private CategoryMapper categoryMapper;

    @Override
    public ResponseBean getCatagoryList(QueryVo queryVo) {
        if (ObjectUtils.isEmpty(queryVo)) {
            return ResponseBean.error(4008, "参数错误(空值)");
        }
        if (queryVo.getPageNum() == null) {
            queryVo.setPageNum(VarConstant.PAGE_NUM);
        }
        if (queryVo.getPageSize() == null) {
            queryVo.setPageSize(VarConstant.PAGE_SIZE);
        }
        QueryWrapper<Category> queryWrapper = null;
        String key = queryVo.getKey();
        if (StringUtils.hasLength(key)) {
            queryWrapper = new QueryWrapper<>();
            queryWrapper.like("category_name",key);
        }
        Page<Category> page = new Page<>(queryVo.getPageNum(), queryVo.getPageSize());
        Page<Category> checkInPage = categoryMapper.selectPage(page, queryWrapper);
        return ResponseBean.success(checkInPage);
    }

    @Override
    public ResponseBean getCategoryBase() {
        List<Category> categories = categoryMapper.selectList(null);
        List<String> key1 = new ArrayList<>();
        List<Integer> val2 = new ArrayList<>();
        for (Category category : categories) {
            key1.add(category.getCategoryName());
            val2.add(category.getFrequency());
        }
        return ResponseBean.success(new CategoryBaseDTO(key1, val2));
    }


}
